package com.qf.blogsystem.controller.system;

import com.alibaba.fastjson.JSON;
import com.qf.blogsystem.core.controller.BaseServlet;
import com.qf.blogsystem.core.utils.BaseConstants;
import com.qf.blogsystem.entity.Admin;
import com.qf.blogsystem.service.AdminService;
import com.qf.blogsystem.service.impl.AdminServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/SystemAdminServlet/*")
public class SystemAdminServlet extends BaseServlet {
    /**
     * 创建管理员业务层实现对象
     */
    private AdminService adminService=new AdminServiceImpl();

    /**
     * 查询所有数据
     * @throws Exception
     */
    public void list() throws Exception {
        //查询数据
        List<Admin> adminList = adminService.list();
        //把数据放到Map中
        HashMap<String, Object> map = new HashMap();
        map.put("adminList",adminList);
        //响应模板视图
        outView("system/systemAdmin/adminList",map);
    }

    /**
     * 添加管理员信息
     * @return String
     * @throws Exception
     */
    public String addAdmin() throws Exception {
        String json = request.getReader().readLine();
        Admin admin = JSON.parseObject(json, Admin.class);
        boolean result = adminService.save(admin);
        if (result){
            return outJson(200,"添加成功!");
        }else {
            return BaseConstants.BASE_SCRIPT + ":添加失败";
        }
    }

    /**
     * 根据id查询管理员信息
     * @throws Exception
     */
    public void selectAdminById() throws Exception {
        String id = request.getParameter("id");
        Admin admin = adminService.byId(Integer.parseInt(id));
        Map<String, Object> map = new HashMap<>();
        map.put("admin", admin);
        outView("system/systemAdmin/adminUpdate", map);
    }

    /**
     * 修改管理员信息
     * @return String
     * @throws Exception
     */
    public String update() throws Exception {
        String json = request.getReader().readLine();
        Admin admin = JSON.parseObject(json, Admin.class);
        boolean result = adminService.update(admin);
        if (result){
            return outJson(200,"修改成功!");
        }else {
            return BaseConstants.BASE_SCRIPT + ":修改失败";
        }
    }

    /**
     * 删除管理员信息
     * @return String
     * @throws Exception
     */
    public String delete() throws Exception {
        String id = request.getParameter("id");
        boolean result = adminService.remove(Integer.parseInt(id));
        return BaseConstants.BASE_REDIRECT + ":" + "SystemAdminServlet/list";
    }

    /**
     * 批量删除管理员信息
     * @return String
     * @throws Exception
     */
    public String batchDelete() throws Exception {
        String json = request.getReader().readLine();
        List<Integer> list = JSON.parseArray(json, Integer.class);
        boolean result = adminService.batchRemove(list);
        if (result){
            return outJson(200,"删除成功!");
        }else {
            return BaseConstants.BASE_SCRIPT + ":删除失败";
        }
    }
}
